﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DLL.Entities;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace MIS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Missile missile = new Missile();
        Targets target = new Targets();
        private void Form1_Load(object sender, EventArgs e)
        {
            TextReader reader = null;
            try
            {

                //Deserializes MIS_Config.xml to get the status of the MIS system  
                reader = new StreamReader("../../XML/Mis_Config.xml");
                XmlSerializer serializer = new XmlSerializer(typeof(Missile));
                missile = (Missile)serializer.Deserialize(reader);

                reader.Close();


            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


            Mis_left.Text = Convert.ToString(target.MissileQty);

            EventScanner.Enabled = true;

        }

        private void Readload_click(object sender, EventArgs e)
        {
            Mis10.Visible = true;
            Mis9.Visible = true;
            Mis8.Visible = true;
            Mis7.Visible = true;
            Mis6.Visible = true;
            Mis5.Visible = true;
            Mis4.Visible = true;
            Mis3.Visible = true;
            Mis2.Visible = true;
            Mis1.Visible = true;
            Slot10.Visible = false;
            Slot9.Visible = false;
            Slot8.Visible = false;
            Slot7.Visible = false;
            Slot6.Visible = false;
            Slot5.Visible = false;
            Slot4.Visible = false;
            Slot3.Visible = false;
            Slot2.Visible = false;
            Slot1.Visible = false;
            target.MissileQty = 10;
            XmlSerializer serializer = new XmlSerializer(typeof(Targets));
            TextWriter textWriter = new StreamWriter("../../../COMM/XML/COM_Event.xml");
            serializer.Serialize(textWriter, target);
            textWriter.Close();
        }

        private void EventScanner_Tick(object sender, EventArgs e)
        {
            TextReader reader = null;

            try
            {
                reader = new StreamReader("../../../COMM/XML/COM_Event.xml");
                XmlSerializer serializer = new XmlSerializer(typeof(Targets));
                target = (Targets)serializer.Deserialize(reader);
                reader.Close();
                txtModel.Text = Convert.ToString(missile.Model);
                txtVersion.Text = Convert.ToString(missile.Version);
                Mis_left.Text = Convert.ToString(target.MissileQty);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            TextReader reader1 = null;

            try
            {
                reader1 = new StreamReader("../../../COMM/XML/COM_Event.xml");
                XmlSerializer serializer = new XmlSerializer(typeof(Targets));
                target = (Targets)serializer.Deserialize(reader1);
                reader1.Close();
                txtboxTarget.Text = target.TargetName;
                txtboxAltitude.Text = Convert.ToString(target.TargetAltitude);
                txtboxDirection.Text = Convert.ToString(target.TargetDirection);
                txtboxHeading.Text = target.TargetNav;
                txtboxRange.Text = Convert.ToString(target.TargetDistance);
                txtboxSpeed.Text = Convert.ToString(target.TargetSpeed);
                Firetimer.Enabled = false;
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            //Checks missile left and displays image based on missile left's #
            #region
            if (Mis_left.Text == "9")
            {
                Mis10.Visible = false;
                Slot10.Visible = true;
                Firetimer.Enabled = true;
            }
            else if (Mis_left.Text == "8")
            {
                Mis10.Visible = false;
                Slot10.Visible = true;
                Mis9.Visible = false;
                Slot9.Visible = true;
            }
            else if (Mis_left.Text == "7")
            {
                Mis10.Visible = false;
                Slot10.Visible = true;
                Mis9.Visible = false;
                Slot9.Visible = true;
                Mis8.Visible = false;
                Slot8.Visible = true;
            }
            else if (Mis_left.Text == "6")
            {
                Mis10.Visible = false;
                Slot10.Visible = true;
                Mis9.Visible = false;
                Slot9.Visible = true;
                Mis8.Visible = false;
                Slot8.Visible = true;
                Mis7.Visible = false;
                Slot7.Visible = true;
            }
            else if (Mis_left.Text == "5")
            {
                Mis10.Visible = false;
                Slot10.Visible = true;
                Mis9.Visible = false;
                Slot9.Visible = true;
                Mis8.Visible = false;
                Slot8.Visible = true;
                Mis7.Visible = false;
                Slot7.Visible = true;
                Mis6.Visible = false;
                Slot6.Visible = true;
            }
            else if (Mis_left.Text == "4")
            {
                Mis10.Visible = false;
                Slot10.Visible = true;
                Mis9.Visible = false;
                Slot9.Visible = true;
                Mis8.Visible = false;
                Slot8.Visible = true;
                Mis7.Visible = false;
                Slot7.Visible = true;
                Mis6.Visible = false;
                Slot6.Visible = true;
                Mis5.Visible = false;
                Slot5.Visible = true;
            }
            else if (Mis_left.Text == "3")
            {
                Mis10.Visible = false;
                Slot10.Visible = true;
                Mis9.Visible = false;
                Slot9.Visible = true;
                Mis8.Visible = false;
                Slot8.Visible = true;
                Mis7.Visible = false;
                Slot7.Visible = true;
                Mis6.Visible = false;
                Slot6.Visible = true;
                Mis5.Visible = false;
                Slot5.Visible = true;
                Mis4.Visible = false;
                Slot4.Visible = true;
            }
            else if (Mis_left.Text == "2")
            {
                Mis10.Visible = false;
                Slot10.Visible = true;
                Mis9.Visible = false;
                Slot9.Visible = true;
                Mis8.Visible = false;
                Slot8.Visible = true;
                Mis7.Visible = false;
                Slot7.Visible = true;
                Mis6.Visible = false;
                Slot6.Visible = true;
                Mis5.Visible = false;
                Slot5.Visible = true;
                Mis4.Visible = false;
                Slot4.Visible = true;
                Mis3.Visible = false;
                Slot3.Visible = true;
            }
            else if (Mis_left.Text == "1")
            {
                Mis10.Visible = false;
                Slot10.Visible = true;
                Mis9.Visible = false;
                Slot9.Visible = true;
                Mis8.Visible = false;
                Slot8.Visible = true;
                Mis7.Visible = false;
                Slot7.Visible = true;
                Mis6.Visible = false;
                Slot6.Visible = true;
                Mis5.Visible = false;
                Slot5.Visible = true;
                Mis4.Visible = false;
                Slot4.Visible = true;
                Mis3.Visible = false;
                Slot3.Visible = true;
                Mis2.Visible = false;
                Slot2.Visible = true;
            }
            else if (Mis_left.Text == "0")
            {
                Mis10.Visible = false;
                Slot10.Visible = true;
                Mis9.Visible = false;
                Slot9.Visible = true;
                Mis8.Visible = false;
                Slot8.Visible = true;
                Mis7.Visible = false;
                Slot7.Visible = true;
                Mis6.Visible = false;
                Slot6.Visible = true;
                Mis5.Visible = false;
                Slot5.Visible = true;
                Mis4.Visible = false;
                Slot4.Visible = true;
                Mis3.Visible = false;
                Slot3.Visible = true;
                Mis2.Visible = false;
                Slot2.Visible = true;
                Mis1.Visible = false;
                Slot1.Visible = true;
            #endregion
            }
        }

        private void Firetimer_Tick(object sender, EventArgs e)
        {
            target.TargetName = "";
            target.TargetAltitude = 0;
            target.TargetDirection = 0;
            target.TargetNav = "";
            target.TargetDistance = 0;
            target.TargetSpeed = 0;
            XmlSerializer serializer = new XmlSerializer(typeof(Targets));
            TextWriter textWriter = new StreamWriter("../../../COMM/XML/COM_Event.xml");
            serializer.Serialize(textWriter, target);
            textWriter.Close();

        }

       
    }
}
